#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <cerrno>
using namespace std;


void SetNoBlock(int fd)
{
    int fl = fcntl(fd,F_GETFL);
    if(fl<0)
    {
        perror("fcntl错误");
        return ;
    }
    fcntl(fd,F_SETFL,fl | O_NONBLOCK);
}


int main()
{
    SetNoBlock(stdin->_fileno);
    while(1)
    {
        char buffer[1024];
        ssize_t res = read(0,buffer,sizeof(buffer) - 1);
        if(res >0 )
        {
            buffer[res] = 0;
            cout<<buffer<<endl;
        }
        else if(res == 0)
        {
            cout<<"stdin end\n";
        }
        else
        {
            if(EAGAIN == errno|| EWOULDBLOCK == errno)//EWOULDBLOCK就是EAGAIN
            {
                cout<<"读区未准备好\n";
            }
            else if(EINTR == errno)
            {
                cout<<"信号中断\n";
            }
            else
            {
                cout<<strerror(errno)<<endl;
            }
        }
        sleep(1);
    }
    return 0;
}